
package com.android.replace;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReplaceTab {

    public static void replaceTab(String file) {
        if (TextUtils.isEmpty(file)) {
            return;
        }
        File destFile = new File(file + ".tmp");
        File sourceFile = new File(file);
        BufferedReader br = null;
        BufferedWriter bw = null;
        boolean success = false;
        try {
            bw = new BufferedWriter(new FileWriter(destFile));
            br = new BufferedReader(new FileReader(sourceFile));
            String line;
            while ((line = br.readLine()) != null) {
                if (TextUtils.isEmpty(line)) {
                    bw.write(line + "\r");
                    System.out.println(line);
                    continue;
                }
                String target = line.replaceAll("	", "   ");
                bw.write(target + "\r");
                System.out.println(target);
            }
            success = true;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            success = false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            success = false;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (success) {
                sourceFile.delete();
                destFile.renameTo(sourceFile);
            }
        }

    }

    public static void main(String[] args) {
        replaceTab("F:\\gittest\\Replace\\src\\com\\android\\replace\\TextUtils.java");
    }

}
